// Copyright (c) 2008, 
// Marc Speck, http://www.faindu.com
// All Rights Reserved. The following is Source Code and is subject to all restrictions 
// on such code as contained in the license accompanying this product.
package com.faindu.editImage.base
{
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
/**
 * Stores possible positions for controls including center, top, right, etc.
 * All positions are in the inner coordinate system.
 *  
 * @see TransformTool.targetBounds 
*/
public class ControlsPositions
{
	private var _topLeft:Point = new Point()
	public function get topLeft(): Point 
	{ 
		return _topLeft.clone() 
	}
	
	private var _top:Point = new Point()	
	public function get top(): Point 
	{
		return _top.clone() 
	}
	
	private var _topRight:Point = new Point()	
	public function get topRight(): Point 
	{ 
		return _topRight.clone() 
	}
	
	private var _right:Point = new Point()	
	public function get right(): Point 
	{ 
		return _right.clone() 
	}
	
	private var _bottomRight:Point = new Point()	
	public function get bottomRight(): Point 
	{ 
		return _bottomRight.clone() 
	}
	
	private var _bottom:Point = new Point()	
	public function get bottom(): Point 
	{ 
		return _bottom.clone() 
	}
	
	private var _bottomLeft:Point = new Point()	
	public function get bottomLeft(): Point 
	{ 
		return _bottomLeft.clone() 
	}
	
	private var _left:Point = new Point()	
	public function get left(): Point 
	{ 
		return _left.clone() 
	}
	
	private var _center:Point = new Point()	
	public function get center(): Point 
	{ 
		return _center.clone() 
	}

	public function get width(): Number
	{
		return topRight.x - topLeft.x
	}

	public function get height(): Number
	{
		return bottomLeft.y - topLeft.y
	}
	
	/**
	 * Calculate the outer most point of the image. <code>topLeftSnapPoint</code>
	 * equals <code>topLeft</code> as long the as the image is not rotated.
	 */
	public function get topLeftSnapPoint(): Point
	{
		var min_x: Number = Math.min( _topLeft.x, _topRight.x, _bottomRight.x, _bottomLeft.x )
		var min_y: Number = Math.min( _topLeft.y, _topRight.y, _bottomRight.y, _bottomLeft.y )
		return new Point( min_x, min_y )
	}
	
	/**
	 * Calculate the outer most point of the image. <code>bottomRightSnapPoint</code>
	 * equals <code>bottomRight</code> as long the as the image is not rotated.
	 */	
	public function get bottomRightSnapPoint(): Point
	{
		var max_x: Number = Math.max( _topLeft.x, _topRight.x, _bottomRight.x, _bottomLeft.x )
		var max_y: Number = Math.max( _topLeft.y, _topRight.y, _bottomRight.y, _bottomLeft.y )
		return new Point( max_x, max_y )
	}
	
	/**
	 * Upadte the positions.
	 */
	public function update( toolMatrix:Matrix, imageBound:Rectangle ): void 
	{
		_topLeft = 		toolMatrix.transformPoint( new Point(imageBound.left, imageBound.top) )
		_topRight = 	toolMatrix.transformPoint( new Point(imageBound.right, imageBound.top) )
		_bottomRight = 	toolMatrix.transformPoint( new Point(imageBound.right, imageBound.bottom) )
		_bottomLeft = 	toolMatrix.transformPoint( new Point(imageBound.left, imageBound.bottom) )
		
		_top = 		Point.interpolate(_topLeft, _topRight, .5)
		_right = 	Point.interpolate(_topRight, _bottomRight, .5)
		_bottom = 	Point.interpolate(_bottomRight, _bottomLeft, .5)
		_left = 	Point.interpolate(_bottomLeft, _topLeft, .5)
		_center = 	Point.interpolate(_topLeft, _bottomRight, .5)
	}
}
}